<template>
  <div class="payment-config-management">
    <el-card>
      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :model="searchForm" inline>
          <el-form-item label="日期范围">
            <el-date-picker
              v-model="searchForm.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              clearable
            />
          </el-form-item>
          <el-form-item label="类型">
            <el-select
              v-model="searchForm.type"
              placeholder="请选择类型"
              clearable
              style="width: 120px"
            >
              <el-option label="类型1" :value="1" />
              <el-option label="类型2" :value="2" />
              <el-option label="类型3" :value="3" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格 -->
      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="名称" min-width="120" />
        <el-table-column prop="alias" label="别名" min-width="120" />
        <el-table-column prop="query" label="查询参数" min-width="150" />
        <el-table-column prop="rate" label="费率" width="100">
          <template #default="{ row }">
            {{ (row.rate / 100).toFixed(2) }}%
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="100">
          <template #default="{ row }">
            <el-tag :type="getTypeTagType(row.type)">
              {{ getTypeText(row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.state === 0 ? 'success' : 'danger'">
              {{ row.state === 0 ? "启用" : "禁用" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-area">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getPaymentConfigList } from '@/api/system'

// 支付配置数据接口
interface PaymentConfig {
  alias: string
  id: number
  name: string
  query: string
  rate: number
  state: number
  type: number
}

// 搜索表单
const searchForm = reactive({
  dateRange: null as [string, string] | null,
  type: null as number | null
})

// 表格数据
const tableData = ref<PaymentConfig[]>([])
const loading = ref(false)

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 获取数据
const loadData = async () => {
  loading.value = true
  try {
    const params = {
      dateRange: searchForm.dateRange ? 1 : 0, // 如果有日期范围则传1，否则传0
      type: searchForm.type || 0
    }
    
    const res:any = await getPaymentConfigList(params)
    if (res && res.data) {
      tableData.value = Array.isArray(res.data) ? res.data : []
      pagination.total = res.total || tableData.value.length
    } else {
      tableData.value = []
      pagination.total = 0
    }
  } catch (error) {
    console.error('获取支付配置列表失败:', error)
    ElMessage.error('获取数据失败')
    tableData.value = []
    pagination.total = 0
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  loadData()
}

// 重置
const handleReset = () => {
  searchForm.dateRange = null
  searchForm.type = null
  pagination.page = 1
  loadData()
}

// 分页大小改变
const handleSizeChange = (size: number) => {
  pagination.size = size
  pagination.page = 1
  loadData()
}

// 当前页改变
const handleCurrentChange = (page: number) => {
  pagination.page = page
  loadData()
}

// 编辑
const handleEdit = (row: PaymentConfig) => {
  ElMessage.info(`编辑配置: ${row.name}`)
  // TODO: 实现编辑功能
}

// 删除
const handleDelete = (row: PaymentConfig) => {
  ElMessageBox.confirm(
    `确定要删除配置"${row.name}"吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 实现删除功能
    ElMessage.success('删除成功')
    loadData()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 获取类型标签类型
const getTypeTagType = (type: number) => {
  const typeMap: Record<number, string> = {
    1: 'primary',
    2: 'success',
    3: 'warning'
  }
  return typeMap[type] || 'info'
}

// 获取类型文本
const getTypeText = (type: number) => {
  const typeMap: Record<number, string> = {
    1: '类型1',
    2: '类型2',
    3: '类型3'
  }
  return typeMap[type] || `类型${type}`
}

// 页面加载时获取数据
onMounted(() => {
  loadData()
})
</script>

<style scoped>
.payment-config-management {
  padding: 20px;
}

.search-area {
  margin-bottom: 20px;
}

.pagination-area {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
